package com.kelvin.oocl.crms.main;

import java.util.Scanner;

import com.kelvin.oocl.crms.entity.Condition;
import com.kelvin.oocl.crms.entity.Customer;
import com.kelvin.oocl.crms.util.MyLinkList;

public class MainView {
	private static ICustomerDao customerDao = new CustomerDaoImpl();
	private static String commonds = "LADUOQH";
	public static void main(String[] args) {
		System.out.println("You can input the follow commond to do opetation!");
		showHelp();
		Scanner scan = new Scanner(System.in);
		String input = null;
		while(true) {
			System.out.print("pls-input>");
			input = scan.nextLine();
//			System.out.println(input);
			int sta = input.indexOf(" ");
			String s = new String(input);
			if(sta != -1){
				s = input.substring(0, sta);
			}
			if(!commonds.contains(s)){
				System.out.println("  "+s+" is not a conmond,pls input the correct commond!");
			}else {
				switch (s) {
				case "L":	//查看客户信息
					if(s.equals(input.trim())){
						MyLinkList<Customer> customers = customerDao.findAll();
						showCustomerList(customers);
					}else {
						String info = input.substring(sta, input.length()).trim();
						String[] pros = info.split(",");
						MyLinkList<Condition> conditions = new MyLinkList<>();
						Condition condition = null;
						boolean flag = false;
						int id = 0;
						for (String pro : pros) {
							String[] str = pro.split(":");
							condition = new Condition();
							condition.setKey(str[0]);
							condition.setValue(str[1]);
							conditions.add(condition);
							if("id".equals(str[0])){
								flag = true;
								id = Integer.parseInt(str[1]);
								break;
							}
						}
						if(flag){
							Customer customer = customerDao.findById(id);
							if(null == customer){
								System.out.println("  Can't not find the customer info of id "+id);
							}else {
								System.out.println("  "+customer);
							}
						}else {
							MyLinkList<Customer> customers = customerDao.findByConditions(conditions);
							showCustomerList(customers);
						}
					}
					break;
				case "A":	//增加客户信息
					if(s.equals(input.trim())){
						System.out.println("  Pls append the customer info after 'A'");
					}else {
						String info = input.substring(sta, input.length()).trim();
						String[] pros = info.split(",");
						Customer customer = getCustomer(pros);
						boolean result = customerDao.add(customer);
						if(result){
							System.out.println("  Add success!");
						}else {
							System.out.println("  Add fail!");
						}
					}
					break;
				case "D":	//删除客户信息
					if(s.equals(input.trim())){
						System.out.println("  Pls append the customer id after 'D'");
					}else {
						try {
							String idstr = input.substring(sta, input.length()).trim();
							int id = Integer.parseInt(idstr);
							boolean result = customerDao.deleteById(id);
							if(result){
								System.out.println("  Delete success!");
							}else {
								System.out.println("  Delete fail!");
							}
						} catch (NumberFormatException e) {
							System.out.println("  Pls input a Integer of id!");
						}
					}
					break;
				case "U":	//更新客户信息
					if(s.equals(input.trim())){
						System.out.println("  Pls append the customer id after 'D' to update by it!");
					}else {
						String last = input.substring(sta, input.length()).trim();
						int a = last.indexOf(" ");
						if(a==-1){
							System.out.println("  The 'U' commond format is not correct!");
						}else {
							String id = last.substring(0, a);
							String[] pros = last.substring(a, last.length()).trim().split(",");
							Customer customer = getCustomer(pros);
							customer.setId(Integer.parseInt(id));
							boolean result = customerDao.updateById(customer);
							if(result){
								System.out.println("  Update success!");
							}else {
								System.out.println("  Update fail!");
							}
						}
					}
					break;
				case "O":	//排序
					if(s.equals(input.trim())){
						System.out.println("  Pls append the customer id after 'O' to sort by it!");
					}else {
						String last = input.substring(sta, input.length()).trim();
						if("id".equals(last)){
							MyLinkList<Customer> cs = customerDao.sortById();
							if(null !=cs){
								showCustomerList(cs);
							}
						}else if("name".equals(last)){
							MyLinkList<Customer> cs = customerDao.sortByName();
							if(null !=cs){
								showCustomerList(cs);
							}
						}else {
							System.out.println("  Can not sort by "+last);
						}
					}
					break;
				case "Q":	//退出程序
					scan.close();
					System.exit(0);
					break;
				case "H":	//查看命令帮助
					if(s.equals(input.trim())){
						System.out.println("Commond Usage:");
						showHelp();
					}else {
						String last = input.substring(sta, input.length()).trim();
						switch (last) {
						case "A":
							showA();
							break;
						case "L":
							showL();
							break;
						case "D":
							showD();
							break;
						case "U":
							showU();
							break;
						case "O":
							showO();
							break;
						case "Q":
							showQ();
							break;
						default:
							System.out.println("  No such commond of "+last);
							break;
						}
					}
					break;
				default:
					break;
				}
			}
		}
	}
	
	//显示客户列表信息
	private static void showCustomerList(MyLinkList<Customer> customers) {
		if(customers.getSize()==0){
			System.out.println("  Can't not find the customer info!");
		}else {
			for (int i = 0; i < customers.getSize(); i++) {
				try {
					System.out.println("  "+customers.get(i));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	//解析用户信息字符串
	public static Customer getCustomer(String[] pros){
		Customer customer = new Customer();
		for (String pro : pros) {
			String[] str = pro.split(":");
			if(str[0].equals("name")){
				customer.setName(str[1]);
			}else if(str[0].equals("sex")){
				customer.setSex(str[1]);
			}else if(str[0].equals("address")){
				customer.setAddress(str[1]);
			}else if(str[0].equals("birthDate")){
				customer.setBirthDate(str[1]);
			}else if(str[0].equals("phoneNumber")){
				customer.setPhoneNumber(str[1]);
			}
		}
		return customer;
	}
	
	public static void showHelp(){
		showL();
		showA();
		showD();
		showU();
		showO();
		showQ();
		System.out.println("  H [command]  ==>to get operation instruction of the commond and you can view all operation");
		System.out.println("                  instructions of all commond by only 'H'!");
	}

	private static void showQ() {
		System.out.println("  Q  ==>to stop the program and quit!");
	}

	private static void showO() {
		System.out.println("  O [id or name]  ==>to sort the customers list by id or name!");
	}

	private static void showU() {
		System.out.println("  U [id] [name:xx,sex:xx...]  ==>to update the customer info by id!");
	}

	private static void showD() {
		System.out.println("  D [id]  ==>to delete a customer by id!");
	}

	private static void showA() {
		System.out.println("  A [name:xx,sex:xx...]  ==>to add a customer info!");
	}

	private static void showL() {
		System.out.println("  L [name]  ==>to show the costomer info by name and you can show all customers by only 'L'!");
	}
}
